<?php

declare(strict_types=1);

namespace App\Overwrites;

use Hyperf\HttpMessage\Exception\MethodNotAllowedHttpException;
use Hyperf\HttpMessage\Exception\NotFoundHttpException;
use Hyperf\HttpServer\CoreMiddleware;
use Mine\Annotation\DependProxy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

#[DependProxy(values: [ CoreMiddleware::class ])]
class HttpCoreMiddleware extends \Mine\Middlewares\HttpCoreMiddleware
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        return parent::process($request,$handler)
            ->withHeader('Access-Control-Allow-Headers', 'accept-language,authorization,lang,uid,token,Keep-Alive,User-Agent,Cache-Control,Content-Type,time,x-lang,version,set-token');
    }

    protected function handleNotFound(ServerRequestInterface $request): ResponseInterface
    {
        if (is_api()) {
            throw new NotFoundHttpException();
        }

        return parent::handleNotFound($request);
    }

    protected function handleMethodNotAllowed(array $methods, ServerRequestInterface $request): ResponseInterface
    {
        if (is_api()) {
            throw new MethodNotAllowedHttpException();
        }

        return parent::handleMethodNotAllowed($methods, $request);
    }
}
